﻿package com.zmcn.flowindex.business.title.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.zmcn.flowindex.bean.vo.IndexTitleVO;
import com.zmcn.flowindex.bean.vo.UserVO;
import com.zmcn.flowindex.business.title.service.titleService;
import com.zmcn.flowindex.business.title.vo.IndexInfoVO;
import com.zmcn.flowindex.business.title.vo.ShowVO;
import com.zmcn.flowindex.fw.action.BaseAction;
import com.zmcn.flowindex.fw.action.Page;
import com.zmcn.flowindex.fw.util.CodeUtil;
import com.zmcn.flowindex.fw.util.OutUtil;
import com.zmcn.flowindex.pub.Const;

/**
 * 
 * 指标主题Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/title")
@Results({ @Result(name = "viewList", location = Const.JSP_title_list), @Result(name = "viewDetail", location = Const.JSP_title_detail), @Result(name = "viewImport", location = Const.JSP_title_imp),
		@Result(name = "viewFrontMain", location = Const.JSP_front_main), @Result(name = "viewFrontTree", location = Const.JSP_front_tree),
		@Result(name = "viewFrontTab", location = Const.JSP_front_tab) })
public class titleAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(titleAction.class);
	private titleService service;

	@Autowired
	public void setService(titleService service) {
		this.service = service;
	}

	/**
	 * 进入指标主题列表页面
	 * 
	 * @return
	 */
	public String viewList() {
		return "viewList";
	}

	/**
	 * 进入指标导入页面
	 * 
	 * @return
	 */
	public String viewImport() {
		return "viewImport";
	}

	/**
	 * 进入指标明细页面
	 * 
	 * @return
	 */
	public String viewDetail() {
		try {
			HttpServletRequest req = getRequest();
			String titleId = ServletRequestUtils.getStringParameter(req, "id");
			req.setAttribute("titleId", titleId);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewDetail";
	}

	/**
	 * 获取指标主题列表数据
	 * 
	 * @return
	 */
	public String getList() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			Page<ShowVO> page = service.getList(new Page<ShowVO>(req));
			//
			map.put("count", page.getCount());
			map.put("info", page.getResult());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	// 上传文件域对象
	private File[] file;
	// 上传文件名
	private String[] fileFileName;

	public File[] getFile() {
		return file;
	}

	public void setFile(File[] file) {
		this.file = file;
	}

	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}

	/**
	 * 导入数据
	 * 
	 * @return
	 */
	public String imp() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String note = ServletRequestUtils.getStringParameter(req, "note");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.imp(note, fileFileName, file, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取某主题的节点树数据
	 * 
	 * @return
	 */
	public String getTreeByTitle() {
		try {
			HttpServletRequest req = getRequest();
			String titleId = ServletRequestUtils.getStringParameter(req, "titleId");
			JSONArray data = service.getTreeByTitle(titleId);
			OutUtil.outJson(data, getResponse());
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return null;
	}

	/**
	 * 删除指标主题
	 * 
	 * @return
	 */
	public String remove() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			String q_uploadTime = ServletRequestUtils.getStringParameter(req, "q_uploadTime");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.remove(q_uploadTime, userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	// ==================================================

	/**
	 * 进入手机主页面
	 * 
	 * @return
	 */
	public String viewFrontMain() {
		return "viewFrontMain";
	}

	/**
	 * 进入手机主页面
	 * 
	 * @return
	 */
	public String viewFrontTab() {
		try {
			HttpServletRequest req = getRequest();
			List<IndexTitleVO> list = service.getLastTitles();
			req.setAttribute("data", list);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewFrontTab";
	}

	/**
	 * 
	 * 获取前台树数据
	 * 
	 * @return
	 */
	public String viewFrontTree() {
		try {
			HttpServletRequest req = getRequest();
			String titleId = ServletRequestUtils.getStringParameter(req, "titleId");
			List<IndexInfoVO> list = service.getFrontTree(titleId);
			req.setAttribute("data", list);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
		}
		return "viewFrontTree";
	}

}
